package com.ihr360.excel.model.dto;

import com.ihr360.commons.model.entity.StringIdWithTimeAndCompanyId;
import com.ihr360.excel.model.type.SpreadSheetStatus;
import com.ihr360.hibernate.type.json.JsonStringType;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import java.util.List;

/**
 * 电子表格数据
 */
@TypeDefs({
        @TypeDef(name = "json", typeClass = JsonStringType.class)
})
@Entity
@DynamicUpdate
public class SpreadSheet extends StringIdWithTimeAndCompanyId {

    @Fetch(FetchMode.SUBSELECT)
    @OneToMany(mappedBy = "sheet", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<CorrectSpreadsheetRow> rows;

    @Fetch(FetchMode.SUBSELECT)
    @OneToMany(mappedBy = "sheet", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<SpreadsheetJournal> journals;

    /**
     * 文件唯一标识
     */
    @Column(length = 40)
    private String fieldKey;

    /**
     * 处理进度唯一标识
     */
    @Column(length = 200)
    private String progressKey;

    @Column
    @Enumerated(EnumType.STRING)
    private SpreadSheetStatus status = SpreadSheetStatus.INPROGRESS;

    /**
     * 带注释的文件对应的Id(包含正确和错误数据)
     */
    @Column(length = 40)
    private String commentsFileId;


    /**
     * 只包含错误数据的fieldId(同样带注释)
     */
    @Column(length = 40)
    private String errorFileId;

    /**
     * 放了调用自定义校验过程中处理过的结果
     * mediumblob 最大16M
     */
    @Column(columnDefinition = "mediumblob")
    private byte[] convertResult;


    /**
     * 是Date类型的表头
     */
    @Column(columnDefinition = "text")
    @Type(type = "json")
    private List<String> dateHeaders;


    public List<CorrectSpreadsheetRow> getRows() {
        return rows;
    }

    public void setRows(List<CorrectSpreadsheetRow> rows) {

        if (this.rows != null) {
            this.rows.clear();
            this.rows.addAll(rows);
        } else {
            this.rows = rows;
        }
    }

    public List<SpreadsheetJournal> getJournals() {
        return journals;
    }

    public void setJournals(List<SpreadsheetJournal> journals) {
        if (this.journals != null) {
            this.journals.clear();
            this.journals.addAll(journals);
        } else {
            this.journals = journals;
        }

    }

    public String getFieldKey() {
        return fieldKey;
    }

    public void setFieldKey(String fieldKey) {
        this.fieldKey = fieldKey;
    }

    public String getProgressKey() {
        return progressKey;
    }

    public void setProgressKey(String progressKey) {
        this.progressKey = progressKey;
    }

    public SpreadSheetStatus getStatus() {
        return status;
    }

    public void setStatus(SpreadSheetStatus status) {
        this.status = status;
    }

    public String getCommentsFileId() {
        return commentsFileId;
    }

    public void setCommentsFileId(String commentsFileId) {
        this.commentsFileId = commentsFileId;
    }

    public byte[] getConvertResult() {
        return convertResult;
    }

    public void setConvertResult(byte[] convertResult) {
        this.convertResult = convertResult;
    }

    public String getErrorFileId() {
        return errorFileId;
    }

    public void setErrorFileId(String errorFileId) {
        this.errorFileId = errorFileId;
    }

    public List<String> getDateHeaders() {
        return dateHeaders;
    }

    public void setDateHeaders(List<String> dateHeaders) {
        this.dateHeaders = dateHeaders;
    }
}
